<template>
  <view>
    <search ref="search" :current="current" @search="handleSearch" />
    <view
      style="
        position: fixed;
        width: 100%;
        top: 0;
        padding-top: 20rpx;
        background-color: #f4f5f6;
        z-index: 2;
      "
    >
      <uni-segmented-control
        style="width: calc(100% - 40rpx)"
        :current="current"
        :values="['预检', '质检']"
        @clickItem="onClickItem"
      />
    </view>
    <view style="position: relative; margin-top: 90rpx">
      <pre-quality-index
        ref="preQualityIndex"
        :firstLoad="firstLoad"
        :params="paramsPreQuality"
        v-show="current === 0"
      />
      <quality-index
        ref="qualityIndex"
        :firstLoad="firstLoad"
        :params="paramsQuality"
        v-show="current === 1"
      />
    </view>
  </view>
</template>

<script>
import search from './search.vue';
import uniSegmentedControl from '@/components/uni-segmented-control/uni-segmented-control.vue';
import preQualityIndex from './preQualityIndex.vue';
import qualityIndex from './qualityIndex.vue';

export default {
  components: {
    search,
    uniSegmentedControl,
    preQualityIndex,
    qualityIndex
  },
  data() {
    return {
      firstLoad: false,
      current: 0,
      paramsPreQuality: {
        limit: global.$limit,
        page: 1,
        warehouseId: this.$storage.getWarehouseId()
      },
      paramsQuality: {
        limit: global.$limit,
        page: 1,
        warehouseId: this.$storage.getWarehouseId()
      }
    };
  },
  onShow() {
    if (global.$refresh) {
      global.$refresh = false;
      uni.showLoading({
        title: '加载中'
      });
      if (this.current === 0) {
        this.$refs.preQualityIndex.load(this.paramsPreQuality);
      } else if (this.current === 1) {
        this.$refs.qualityIndex.load(this.paramsQuality);
      }
    }
  },
  onLoad() {
    uni.showLoading({
      title: '加载中'
    });
    this.firstLoad = true;
  },
  onNavigationBarButtonTap() {
    const params = Object.assign(
      {},
      this.current === 0 ? this.paramsPreQuality : this.paramsQuality
    );
    this.$refs.search.open(Object.assign({}, params));
  },
  onPullDownRefresh() {
    if (this.current === 0) {
      this.paramsPreQuality.page = 1;
      this.$refs.preQualityIndex.load(this.paramsPreQuality);
    } else if (this.current === 1) {
      this.paramsQuality.page = 1;
      this.$refs.qualityIndex.load(this.paramsQuality);
    }
  },
  onReachBottom() {
    if (this.current === 0) {
      if (
        this.$refs.preQualityIndex.loadMoreStatus ===
        global.$loadMoreStatus.more
      ) {
        this.$refs.preQualityIndex.loadMoreStatus =
          global.$loadMoreStatus.loading;
        this.paramsPreQuality.page++;
        this.$refs.preQualityIndex.load(this.paramsPreQuality);
      }
    } else if (this.current === 1) {
      if (
        this.$refs.qualityIndex.loadMoreStatus === global.$loadMoreStatus.more
      ) {
        this.$refs.qualityIndex.loadMoreStatus = global.$loadMoreStatus.loading;
        this.paramsQuality.page++;
        this.$refs.qualityIndex.load(this.paramsQuality);
      }
    }
  },
  methods: {
    handleSearch(param = {}) {
      uni.showLoading({
        title: '加载中'
      });
      if (this.current === 0) {
        this.paramsPreQuality = Object.assign(
          {
            limit: global.$limit,
            page: 1,
            warehouseId: this.$storage.getWarehouseId()
          },
          param
        );
        this.$refs.preQualityIndex.load(this.paramsPreQuality);
      } else if (this.current === 1) {
        this.paramsQuality = Object.assign(
          {
            limit: global.$limit,
            page: 1,
            warehouseId: this.$storage.getWarehouseId()
          },
          param
        );
        this.$refs.qualityIndex.load(this.paramsQuality);
      }
    },
    onClickItem(e) {
      this.current = e.currentIndex;
    }
  }
};
</script>
